'use strict';

module.exports = function(grunt) {
    // Load auto de tous les modules
    require('load-grunt-tasks')(grunt);

    // Configurable paths for the application
    var appConfig = {
        path : {
            dev : "app",
            tmp : ".tmp",
            dist: 'dist'
        }
    };

    grunt.initConfig({

        pkg: grunt.file.readJSON('package.json'),

        appConfig : appConfig,

       // Dynamic properties configuration
        ngconstant: {
            options :{
                name : "constants"
            },
            // Environment targets
            dev : {
                options: {
                    dest: '<%= appConfig.path.dev %>/scripts/constants/constants.js'
                },
                constants : {
                    ENV :{
                        name : 'dev',
                        helloseriesEndPoint : "http://localhost:8080/helloseries-rest/api/"
                    }
                }
            },
            devdist  :{
                options: {
                    dest: '<%= appConfig.path.dev %>/scripts/constants/constants.js'
                },
                constants : {
                    ENV :{
                        name : 'dev-to-dist',
                        helloseriesEndPoint : "http://62.141.39.8:8080/helloseries-rest/api/"
                    }
                }
            },
            dist : {
                options: {
                    dest: '<%= appConfig.path.tmp %>/scripts/constants/constants.js'
                },
                constants :{
                    ENV :{
                        name : 'dist',
                        helloseriesEndPoint : "/helloseries-rest/api/"
                    }
                }
            }
        },


        // Auto load bower dep to index.html
        wiredep: {
            build : {
                src: '<%= appConfig.path.dev %>/index.html'
            }
        },
        // Ménage
        clean: {
            build : {
                src : ["<%= appConfig.path.dist %>"]
            },
            tmp : {
                src : "<%= appConfig.path.tmp %>"
            }
        },

       // Copie dans un répertoire temporaire de travail
        copy: {
            tmp: {
                expand: true,
                cwd : '<%= appConfig.path.dev %>',
                dest: '<%= appConfig.path.tmp %>',
                src: [
                    '**',
                    //'!bower_components/**',
                    '!scripts/constants/**'
                    //'!styles/**'
                ]
            },
            build: {
                expand: true,
                cwd : '<%= appConfig.path.tmp %>',
                dest: '<%= appConfig.path.dist %>',
                src: [
                    '**',
                    '!bower_components/**',
                    '!scripts/constants/**',
                    // Scripts et styles copiés via usemin
                    '!scripts/**',
                    '!styles/**',
                    // Generé par la tâche concat donc ignoré
                    '!concat/**'
                ]
            }
        },
        //Minification
        useminPrepare: {
            html: '<%= appConfig.path.tmp %>/index.html',
            options: {
                dest: '<%= appConfig.path.dist %>',
                flow: {
                    html: {
                        steps: {
                            js: ['concat', 'uglifyjs'],
                            css: ['cssmin']
                        },
                        post: {}
                    }
                }
            }
        },
         // Tell which file use minified files
        usemin : {
            html : ['<%= appConfig.path.dist %>/index.html']
        },

        // Configuration du packaging
        compress: {
            build: {
                options: {
                    archive: '<%= pkg.name %>-<%= pkg.version %>.zip'
                },
                files: [{
                    expand: true,
                    src: '**',
                    cwd: '<%= appConfig.path.dist %>',
                    dist : "<%= pkg.name %>"
                }]
            }
        }
    });

    // the default task can be run just by typing "grunt" on the command line
    grunt.registerTask('build', [
        'clean',
        // Injection des dépendances bower
        'wiredep',
        // Copie dans le répertoire de travail temporaire
        'copy:tmp',
        // Création du fichier d'environnement de build dans le dossier temporaire
        'ngconstant:dist',
        // Préparation de la minification
        'useminPrepare',
        'concat',
        'cssmin',
        'uglify',
        // Copie dans le répertoire de build
        'copy:build',
        'usemin',
        'clean:tmp',
        // packaging
        'compress'
    ]);

    grunt.registerTask('dev', [
        'ngconstant:dev',
        'wiredep'
    ]);

    grunt.registerTask('devdist', [
        'ngconstant:devdist',
        'wiredep'
    ]);

};